import request from '@/utils/request'
import { removeToken, setToken } from '@/utils/storage'

/**
 * 设置token
 * @param {*} tokenInfo
 * @returns
 */
export const setTokenInfo = (tokenInfo) => ({
  type: 'login_token',
  payload: tokenInfo,
})

export const removeReduxToken = () => ({ type: 'login_removeReduxToken' })

/**
 *  发送验证码
 * @param {*} mobile
 * @returns
 */
export const sendCode = (mobile) => () => request.get(`/sms/codes/${mobile}`)

/**
 * 登录
 * @param {*} data
 * @returns
 */
export const login = (data) => async (dispatch) => {
  try {
    const res = await request.post('/authorizations', data)
    dispatch(setTokenInfo(res.data))
    setToken(res.data)
  } catch (err) {
    console.dir(err)
  }
}

/**
 * 退出
 * @returns
 */
export const logout = () => (dispatch) => {
  removeToken()
  dispatch(removeReduxToken())
  
}
